﻿using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using FibMatrix.BaseUtils;
using Sirenix.OdinInspector;

namespace FibMatrix
{
    /// <summary>
    /// 资源系统的配置选项，有些是用于调试的
    /// </summary>
    [FM_Mono.IgnoreGen]
    public class ResourceDevCfg : FibSingletonCfgBase
    {
        // ------------- 子类必须实现的指定签名的静态单例方法才能显示在ProjectSetting界面
        static ResourceDevCfg s_Inst;

        public static ResourceDevCfg Instance
        {
            get
            {
                if (s_Inst == null)
                    s_Inst = LoadOrCreate<ResourceDevCfg>(runtimeAsset: true);
                return s_Inst;
            }
        }
        // --------------------------------------------

        [LabelText("编辑器使用bundle模式")] [Tooltip("需要先构建bundle。运行会直接读取构建目录中的ab")]
        public bool forceBundleModeInEditor = false;

        [LabelText("同步加载时警告")] public bool logSyncLoadAsWarning = false;
        [LabelText("同步加载时报错")] public bool logSyncLoadAsError = false;

        [LabelText("调试边玩边下")] [Tooltip("同步资源读包内的，异步资源进行下载缓存")]
        // 要序列化，但获取时允许在webgl工程中修改，需要包装起来，所以是private
        [SerializeField]
        private bool debugStreamPlay = false;

        [LabelText("调试用资源HTTP下载地址")] [Tooltip("仅限对构建的资源文件有效，对数据表无效")] [ShowIf("debugStreamPlay")]
        // 要序列化，但获取时允许在webgl工程中修改，需要包装起来，所以是private
        [SerializeField]
        private string debugCDN = "http://192.168.60.116:8000/Upload";

#if UNITY_WEBGL && !UNITY_EDITOR
        [DllImport("__Internal")]
        private static extern string GetDataCdn();

        [DllImport("__Internal")]
        private static extern bool GetJSDebugStreamPlay();
#endif

        /// <summary>
        /// 获取调试用cdn，如果是微信小游戏下，则从Game.js中获取，方便修改调试
        /// </summary>
        /// <returns></returns>
        public string GetDebugCdn()
        {
            var cdn = debugCDN;
#if UNITY_WEBGL && !UNITY_EDITOR
            cdn = GetDataCdn();
#endif
            Debug.Log($"use debug cdn: {cdn}");
            return cdn;
        }

        public bool IsDebugStreamPlay()
        {
#if UNITY_WEBGL && !UNITY_EDITOR
            return GetJSDebugStreamPlay();
#endif
            return debugStreamPlay;
        }
    }
}